package com.jsb.laundry.common.response;

import com.github.pagehelper.Page;
import com.jsb.laundry.common.enums.ReturnStatusCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;

/**
 * 接口的响应统一返回
 */
@ApiModel(value = "WebResponse对象", description = "接口统一返回数据格式")
public class WebResponse<T> implements Serializable {

    private static final long serialVersionUID = -1463052981976633870L;

    /**
     * 状态码
     */
    @ApiModelProperty(value = "响应状态码:200 表示成功", dataType = "int", example = "0")
    private String code;

    /**
     * 提示消息
     */
    @ApiModelProperty(value = "提示信息", dataType = "string")
    private String msg;

    /**
     * 数据
     *
     * @JsonInclude(JsonInclude.Include.NON_NULL) 为空时忽略解析
     * @JsonInclude(JsonInclude.Include.NON_EMPTY) 为""或为NULL时不序列化
     */
    @ApiModelProperty(value = "响应数据 如果为空则 1是成功 0是失败")
    private T data;


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 请求成功，返回响应
     */
    public static WebResponse success() {
        WebResponse result = new WebResponse();
        result.setCode(ReturnStatusCode.SUCCESS.getCode());
        result.setMsg(ReturnStatusCode.SUCCESS.name());
        return result;
    }

    /**
     * 请求成功，返回响应
     */
    public static WebResponse success(String msg) {
        WebResponse result = new WebResponse();
        result.setCode(ReturnStatusCode.SUCCESS.getCode());
        result.setMsg(msg);
        return result;
    }

    /**
     * 自定义返回状态码、消息
     */
    public static WebResponse success(String code, String msg) {
        WebResponse result = new WebResponse();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static WebResponse error(String code, String msg) {
        WebResponse result = new WebResponse();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 请求成功，返回响应数据
     *
     * @param t 要返回的数据
     */
    public static <T> WebResponse<T> successData(T t) {
        WebResponse result = new WebResponse();
        result.setCode(ReturnStatusCode.SUCCESS.getCode());
        result.setMsg(ReturnStatusCode.SUCCESS.name());

        // TODO list类型并且开启了分页时,格式化返回数据为page
        if (t instanceof Page && t instanceof List) {
            ResultPage<T> res = new ResultPage<>(t);
            result.setData(res);
        } else {
            result.setData(t);
        }

        return result;
    }

    /**
     * 请求参数错误 (请求参数为空或参数错误)
     *
     * @return
     */
    public static WebResponse failOfParameter() {
        WebResponse response = new WebResponse();
        response.setCode(ReturnStatusCode.PARAMETER_INCOMPLETE.getCode());
        response.setMsg(ReturnStatusCode.PARAMETER_INCOMPLETE.getMsg());
        return response;
    }
    public static <T> WebResponse successData(String code, String msg,T t) {
        WebResponse result = new WebResponse();
        result.setData(t);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

}